// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DataLoader.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Robotics.IO
{
    using System;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Runtime.Serialization;
    using Microsoft.Robotics.IO;

    /// <summary>
    /// Helper utility class for importing and exporting data in human readable form.
    /// It requires the type to be of class to avoid code bloat in using value types.
    /// Since we control the serializable type for each calibration file, we can make them all classes.
    /// These are read and written only once, so there is not much performance to be gained by making them structs.
    /// If performance is desired, the underlying structs can be passed around directly instead by the consumer of the DataLoader.
    /// </summary>
    /// <typeparam name="T">The type of data to import or export to file</typeparam>
    public static class DataLoader<T> where T : class
    {
        /// <summary>
        /// Import serialized data from file.
        /// </summary>
        /// <param name="filename">File name</param>
        /// <returns>Imported data</returns>
        public static T ImportData(string filename)
        {
            if (File.Exists(filename))
            {
                return Serializer.Load<T>(filename, false);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Export data to file.
        /// </summary>
        /// <param name="filename">File prefix</param>
        /// <param name="data">Data to export</param>
        public static void ExportData(string filename, T data)
        {
            Serializer.Save<T>(filename, data, false);
        }
    }
}
